|
|
Component
AsynchroMaster
Asynchronous serial communication - master
Component Level: High
Category:
CPU Internal Peripherals-Communication
Typical Usage:
(Examples of a typical usage of the component in user code.
For more information please
see the page Component Code
Typical Usage.)
Required component name is "AM1". There are the following three typical usage modes:
(1)
MAIN.C
byte ch;
byte err;
AM1_TError error;
void main(void)
{
AM1_SelectSlave(1); //Select slave with number 1
for(;;) {
//Wait for a character
while(AM1_GetCharsInRxBuf() == 0);
//Read received character and send it if no error is detected
if( AM1_RecvChar(&ch) == ERR_OK) {
AM1_SendChar(ch);
} else {
// The GetBreak method must be used first because
// the GetError method clear the break flag
// This method is available if the Break
// signal is enabled (Advanced view).
AM1_GetBreak(&b);
if (b) {
// the break character was received
}
AM1_GetError(&error)
if(error.errName.Parity)
//Parity error
else if(error.errName.Framing)
//Framing error
.
.
.
}
}
}
(2)
EVENTS.C
void AM1_OnRxChar(void)
{
byte ch;
//Read received character and if no error is detected
if(AM1_RecvChar(&ch) == ERR_OK)
//send it back
AM1_SendChar(ch);
}
(3)
MAIN.C
byte ch;
AM1_TError error; //TError union is defined in the header file
void main(void)
{
AM1_SelectSlave(1); //Select slave with number 1
//Wait for a character
while(AM1_GetCharsInRxBuf() == 0);
//Read received character
AM1_RecvChar(&ch);
//Read the last communication errors
AM1_GetError(&error)
if(error.err == 0) {
//Send the last received character if no error is detected
AM1_SendChar(ch);
} else {
if(error.errName.Parity) {
//Parity error
} else if(error.errName.Framing) {
//Framing error
}
.
.
}
.
.
}
|